package cn.iocoder.yudao.module.infra.controller.admin.codeincrement;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.infra.controller.admin.codeincrement.vo.CodeIncrementPageReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.codeincrement.vo.CodeIncrementRespVO;
import cn.iocoder.yudao.module.infra.controller.admin.codeincrement.vo.CodeIncrementSaveReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.codeincrement.CodeIncrementDO;
import cn.iocoder.yudao.module.infra.service.codeincrement.CodeIncrementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - code自增号")
@RestController
@RequestMapping("/infra/code-increment")
@Validated
public class CodeIncrementController {

    @Resource
    private CodeIncrementService codeIncrementService;

    @PostMapping("/create")
    @Operation(summary = "创建code自增号")
    @PreAuthorize("@ss.hasPermission('infra:code-increment:create')")
    public CommonResult<Long> createCodeIncrement(@Valid @RequestBody CodeIncrementSaveReqVO createReqVO) {
        return success(codeIncrementService.createCodeIncrement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新code自增号")
    @PreAuthorize("@ss.hasPermission('infra:code-increment:update')")
    public CommonResult<Boolean> updateCodeIncrement(@Valid @RequestBody CodeIncrementSaveReqVO updateReqVO) {
        codeIncrementService.updateCodeIncrement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除code自增号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:code-increment:delete')")
    public CommonResult<Boolean> deleteCodeIncrement(@RequestParam("id") Long id) {
        codeIncrementService.deleteCodeIncrement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得code自增号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('infra:code-increment:query')")
    public CommonResult<CodeIncrementRespVO> getCodeIncrement(@RequestParam("id") Long id) {
        CodeIncrementDO codeIncrement = codeIncrementService.getCodeIncrement(id);
        return success(BeanUtils.toBean(codeIncrement, CodeIncrementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得code自增号分页")
    @PreAuthorize("@ss.hasPermission('infra:code-increment:query')")
    public CommonResult<PageResult<CodeIncrementRespVO>> getCodeIncrementPage(@Valid CodeIncrementPageReqVO pageReqVO) {
        PageResult<CodeIncrementDO> pageResult = codeIncrementService.getCodeIncrementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CodeIncrementRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出code自增号 Excel")
    @PreAuthorize("@ss.hasPermission('infra:code-increment:export')")
    @OperateLog(type = EXPORT)
    public void exportCodeIncrementExcel(@Valid CodeIncrementPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CodeIncrementDO> list = codeIncrementService.getCodeIncrementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "code自增号.xls", "数据", CodeIncrementRespVO.class,
                        BeanUtils.toBean(list, CodeIncrementRespVO.class));
    }

}